package com.zyc.app.woa.data;

import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.zyc.app.woa.utils.CommonUtils;

import java.util.ArrayList;

/**
 * Author: yingchang.zhang
 * Date  : 2014/11/27
 */
public class Gzhcb {
    @SerializedName("page")
    public int currPage;
    @SerializedName("items")
    public String[] items;
    @SerializedName("totalItems")
    public int totalItems;
    @SerializedName("totalPages")
    public int totalPages;
    public ArrayList<ArticleItem> articles;

    public static Gzhcb build(String json) {
//        Trace.log("build:" + json);
        Gzhcb gzhcb = null;
        try {
            gzhcb = CommonUtils.getGson().fromJson(json, Gzhcb.class);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }

        if (null == gzhcb) {
            return null;
        }

        ArticleItem articleItem;
        for (String string : gzhcb.items) {
            articleItem = ArticleItem.parse(string);
            gzhcb.addArticle(articleItem);
//                Utils.log("----------------------------- ");
//                Utils.log(articleItem.toString());
        }
        return gzhcb;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("page: " + currPage + "\n");
        sb.append("items.length: " + items.length + "\n");
        sb.append("totalItems: " + totalItems + "\n");
        sb.append("totalPages: " + totalPages + "\n");
        sb.append("collect articles: " + (null!= articles ? articles.size() : 0) + "\n");

        if (null != articles) {
            for (ArticleItem articleItem : articles) {
                sb.append(articleItem.toString());
            }
        }

        return sb.toString();
    }

    public void addArticle(ArticleItem articleItem) {
        if (null == articles) {
            articles = new ArrayList<ArticleItem>();
        }

        articles.add(articleItem);
    }

    public void addArticles(ArrayList<ArticleItem> articles) {
        if (null == this.articles) {
            this.articles = new ArrayList<ArticleItem>();
        }

        if (null != articles) {
            this.articles.addAll(articles);
        }
    }

    public int getArticleCount() {
        return null != articles ? articles.size() : 0;
    }
}


